package com.huluobo.a2207douyindemo.register;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2207douyindemo.R;
import com.huluobo.a2207douyindemo.databinding.ActivityRegisterBinding;
import com.huluobo.base.bean.UserBeanResult;
import com.huluobo.base.mvvm.BaseMVVMActivity;

public class RegisterActivity extends BaseMVVMActivity<ActivityRegisterBinding, RegisterViewModel> {

    @Override
    protected void initData() {
        vm.registerSuccess.observe(this, userBeanResult -> {
            ToastUtils.showLong(userBeanResult.getMessage());
            if (userBeanResult.getCode() == 200) {
                finish();
            }
        });

        vm.registerFailed.observe(this, s -> {
            Log.i("TagA", "注册失败:" + s);
        });
    }

    @Override
    protected void initView() {
        binding.registerHeader.headerTitle.setText("注册");
        binding.registerHeader.headerBack.setOnClickListener(v -> finish());

        binding.registerBtn.setOnClickListener(v -> {
            if (binding.registerUsername.getText().toString().isEmpty() ||
                    binding.registerPassword.getText().toString().isEmpty() ||
                    binding.registerRePassword.getText().toString().isEmpty()) {
                ToastUtils.showLong("注册项不能有空值");
            } else {
                vm.register(binding.registerUsername.getText().toString(),
                        binding.registerPassword.getText().toString(), binding.registerRePassword.getText().toString());
            }
        });
    }

    @Override
    protected Class<RegisterViewModel> getViewModel() {
        return RegisterViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }
}